/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package iamutkarshtiwari.github.io.ananas.editimage.adapter;

import iamutkarshtiwari.github.io.ananas.ResourceTable;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * 颜色选择adapter
 */
public class ColorPickerOwnAdapter extends BaseItemProvider {

    private List<Integer> colorPickerColors;
    private Context mContext;
    private Component showView;

    /**
     * 构造方法
     *
     * @param context Context
     */
    public ColorPickerOwnAdapter(Context context) {
        super();
        this.mContext = context;
        this.colorPickerColors = initColor();
    }

    @Override
    public int getCount() {
        return colorPickerColors.size();
    }

    @Override
    public Object getItem(int position) {
        return colorPickerColors.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Component component1 = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_color_picker_item_list, null, false);
        showView = component1.findComponentById(ResourceTable.Id_color_picker_view);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(colorPickerColors.get(position).intValue()));
        showView.setBackground(shapeElement);
        return component1;
    }

    /**
     * 返回列表对应位置的颜色
     *
     * @param position 位置
     * @return 返回列表对应位置的颜色
     */
    public int getColor(int position) {
        return colorPickerColors.get(position);
    }


    private List<Integer> initColor() {
        List<Integer> list = new ArrayList<>();
        list.add(Color.getIntColor("#ffF2F3F4"));
        list.add(Color.getIntColor("#ff1d1d1d"));
        list.add(Color.getIntColor("#ffebce2b"));
        list.add(Color.getIntColor("#ff702c8c"));
        list.add(Color.getIntColor("#ffdb6917"));
        list.add(Color.getIntColor("#ff96cde6"));
        list.add(Color.getIntColor("#ffba1c30"));
        list.add(Color.getIntColor("#ffc0bd7f"));
        list.add(Color.getIntColor("#ff7f7e80"));
        list.add(Color.getIntColor("#ff5fa641"));
        list.add(Color.getIntColor("#ffd485b2"));
        list.add(Color.getIntColor("#ff4277b6"));
        list.add(Color.getIntColor("#ffdf8461"));
        list.add(Color.getIntColor("#ff463397"));
        list.add(Color.getIntColor("#ffe1a11a"));
        list.add(Color.getIntColor("#ff91218c"));
        list.add(Color.getIntColor("#ffe8e948"));
        list.add(Color.getIntColor("#ff7e1510"));
        list.add(Color.getIntColor("#ff92ae31"));
        list.add(Color.getIntColor("#ff6f340d"));
        list.add(Color.getIntColor("#ffd32b1e"));
        list.add(Color.getIntColor("#ff2b3514"));

        return list;
    }

}
